package com.flute.framework.index.fieldbuilder;

import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;

import com.flute.framework.spliter.WordSpliter;
import com.flute.framework.stringprocessor.SplitStringProcessor;
import com.flute.tools.data.DataRecord;
import com.flute.tools.util.StringUtil;

public class MultiFieldSplitedBuilder extends AbstractLuceneFieldBuilder {

	public Object buildField(DataRecord record) {
		String[] fields = getDataField().split(",");
		
		StringBuilder sb = new StringBuilder("");
		for(String field:fields) {
			sb.append(record.getFieldValue(field)).append(" ");
		}
		String value = sb.toString();
		
		if (value == null) {
			value = "";
		}
		value = StringUtil.chineseFJChange(value);
		value = StringUtil.getStandardString(value);
		value = process(value);

		return new Field(docField, value, getFieldStore(), getFieldIndex());
	}
	
	@Override
	public Index getFieldIndex() {
		return Field.Index.ANALYZED;
	}

	@Override
	public Store getFieldStore() {
		return Field.Store.NO;
	}

	@Override
	public String process(String string) {
		return new SplitStringProcessor(WordSpliter.getInstance())
				.process(string);
	}
}
